package taliav3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

/**
 *
 *
 * Klasa zawiera statyczne metody typu MouseAdapter/MouseMotion
 * metody sa potrzebne by obslugiwac klikniecia mysza (np. dodawanie kart
 * do rzutu, pobieranie kart, zamiana jokera itp.)
 * Ilosc metod i ich objetosc jest spora, ale wywyolywanie ich to kwestia tylko
 * 1 linii (zwieksza sie czytelnosc dzialania aplikacji ogolnie).
 *
 */
public class MouseAdapters {

    static JFrame frame;
    static String path;
    static DrawTable table1;
    static MainMenu menu1;
    static DrawTransformation sub_panel;
    static int p_index, ind,Stage;
    static boolean Next;
    /** Handling of clicking sub-menu sort.*/
    protected static MouseAdapter sortAction(String Path, DrawTable table)
    {
        path=Path;
        table1=table;
        return new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setIcon(new ImageIcon(getClass().getResource(path+".png")));
                int CordY = 450, index=0;
                for(int i=0; i<4; i++, CordY+=49)
                {
                    if(CordY==label.getY())
                    {
                        index=i;
                        break;
                    }
                }
                int p_index=1;
                if(table1.Server==true)
                    p_index=0;
                Util.SortCards(p_index, index+1, table1.players);
                table1.menu[3].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_sortujDis.png")));
                table1.ReDrawComponents(true,0);
                table1.panel.remove(table1.BG);
                table1.panel.add(table1.BG);
                table1.status.setOpaque(true);
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
        };
    }
    /** Handling of clicking sub-menu demand (when Ace is selected) or setting color of Joker.*/
    protected static MouseAdapter pickColorAction(final String path, final char color, DrawTable table, DrawTransformation subpanel)
    {
        table1=table;
        sub_panel = subpanel;
        return new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                sub_panel.color_selected=color;
                char kolor[] = new char[]{'H','D','S','C'};
                for(int i=0; i<4; i++)
                {
                    String pathT = "/taliav3/img/"+kolor[i]+"mW.png";
                    table1.colors[i].setIcon(new ImageIcon(getClass().getResource(pathT)));
                }
                table1.panel.remove(table1.BG);
                if(sub_panel.selected.rank=='A')
                    table1.BackUpDemand=color;
                else if(sub_panel.selected.rank=='B')
                {
                    sub_panel.Jcolor=color;
                    label.setIcon(new ImageIcon(getClass().getResource(path+".png")));
                    if(sub_panel.Jrank!='0')
                    {
                        table1.players[sub_panel.p_index].TransformJoker
                                (sub_panel.ind, sub_panel.Jcolor, sub_panel.Jrank,
                                Card.SetPunish(sub_panel.Jrank, sub_panel.Jcolor));
                        for(int i=0; i<table1.Selected.size(); i++)
                        {
                            if(sub_panel.ind==table1.Selected.get(i).ind)
                            {
                                table1.Selected.set(i, new Card(table1.players[sub_panel.p_index].getCard(sub_panel.ind)));
                                table1.ReDrawComponents(true,0);
                                table1.panel.remove(table1.BG);
                                table1.panel.add(table1.BG);
                                table1.status.setOpaque(true);
                            }
                        }
                    }
                }
                if(sub_panel.selected.rank=='A' || (sub_panel.selected.rank!='B' && sub_panel.selected.rank!='0'))
                {
                    label.setIcon(new ImageIcon(getClass().getResource(path+"B.png")));
                    table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandDis.png")));
                    for(int i=0; i<4; i++)
                        table1.panel.remove(table1.colors[i]);
                    table1.panel.remove(table1.bgDC);
                    table1.panel.add(table1.BG);
                    table1.panel.repaint();
                }
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label.setIcon(new ImageIcon(getClass().getResource(path+"B.png")));
            }
            public void mouseExited(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                if(sub_panel.color_selected!=color)
                    label.setIcon(new ImageIcon(getClass().getResource(path+"W.png")));
            }
        };
    }
    /** Handling of clicking sub-menu demand (when Jack is selected) or setting rank of Joker.*/
    protected static MouseAdapter pickRankAction(final String path, final char rank, DrawTable table, DrawTransformation subpanel)
    {
        table1 = table;
        sub_panel = subpanel;
        return new MouseAdapter ()
        {
            public void mouseClicked(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                sub_panel.rank_selected=rank;
                table1.panel.remove(table1.BG);
                if(sub_panel.selected.rank=='J')
                {
                    if(rank!='D')
                        table1.BackUpDemand=rank;
                    else if(rank=='D')
                        table1.BackUpDemand='Q';
                    table1.BackUpDemandQueue=table1.talia.players;
                }
                else if(sub_panel.selected.rank=='B')
                {
                    char figuraPL[] = new char[]{'2', '3', '4', '5', '6', '7', '8', '9', '1', 'W', 'D', 'K', 'A'};
                    for(int i=0; i<figuraPL.length; i++)
                        table1.ranks[i].setForeground(Color.white);
                    label.setForeground(Color.yellow);
                    sub_panel.Jrank=rank;
                    if(rank=='D')
                        sub_panel.Jrank='Q';
                    else if(rank=='W')
                        sub_panel.Jrank='J';
                    if(sub_panel.Jcolor!='0')
                        table1.players[p_index].TransformJoker(ind, sub_panel.Jcolor, sub_panel.Jrank,
                                Card.SetPunish(sub_panel.Jrank, sub_panel.Jcolor));
                }
                if(sub_panel.selected.rank=='J' || (sub_panel.selected.rank!='B' && sub_panel.selected.rank!='0'))
                {
                    table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandDis.png")));
                    for(int i=0; i<table1.ranks.length; i++)
                        table1.panel.remove(table1.ranks[i]);
                    table1.panel.remove(table1.bgDR);
                    table1.panel.add(table1.BG);
                    table1.panel.repaint();
                }
            }
            @Override
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label.setForeground(Color.yellow);
            }
            public void mouseExited(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                if(sub_panel.rank_selected!=rank)
                    label.setForeground(Color.white);
            }
        };
    }
    /** Handling of clicking in Card or doubleCliking in Card. Showing user wheter the move with selected card is possible.*/
    protected static MouseAdapter pickCardAction(DrawTable table, int p_indexx)
    {
        table1 = table;
        p_index = getCurrent(table1);
        return new MouseAdapter ()
        {
            public void mouseClicked(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                ind = table1.GetIndex(p_index,label.getX(),table1.players[p_index].X_width);
                boolean obecne=false;
                table1.jokers=false;
                table1.as=false;
                table1.walet=false;
                for(int i=0; i<table1.Selected.size(); i++)
                {
                    if(table1.Selected.get(i).rank=='B')
                        table1.jokers=true;
                    else if(table1.Selected.get(i).rank=='J')
                        table1.walet=true;
                    else if(table1.Selected.get(i).rank=='A')
                        table1.as=true;
                    if(table1.Selected.get(i).ind == ind && table1.Selected.get(i).rank!=0)
                    {
                        obecne=true;
                        break;
                    }
                }

                if(me.getClickCount()%2==1 && obecne==false && table1.jokers==false)
                {
                    if((table1.Selected.size()> 0 && (table1.Selected.get(0).rank==table1.players[p_index].getCard(ind).rank
                        || table1.players[p_index].getCard(ind).rank=='B'))
                        || (table1.Selected.size()==0 && table1.players[p_index].MovePossible(ind, table1.talia)))
                    {
                        if(table1.players[p_index].getCard(ind).rank=='B')
                        {
                            table1.panel.remove(table1.BG);
                            table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_jokerWyb.png")));
                            DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                            sub.DrawColors();
                            sub.DrawRanks();
                            table1.panel.add(table1.BG);
                            table1.menu[4].addMouseListener(new MouseAdapter ()
                            {
                                public void mousePressed(MouseEvent me)
                                {
                                    table1.panel.remove(table1.BG);
                                    if(me.getClickCount()%2==1)
                                    {
                                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_jokerDis.png")));
                                        table1.BackUpDemand=table1.players[p_index].getCard(ind).color;
                                        for(int i=0; i<4; i++)
                                            table1.panel.remove(table1.colors[i]);
                                        for(int i=0; i<8; i++)
                                            table1.panel.remove(table1.ranks[i]);

                                        try {   table1.panel.remove(table1.bgDC);   } catch(Exception e){};
                                        try {   table1.panel.remove(table1.bgDR);  } catch(Exception e){};
                                        try
                                        {
                                            for(int i=8; i<13; i++)
                                                table1.panel.remove(table1.ranks[i]);
                                        }
                                        catch(Exception e){};
										table1.panel.repaint();
                                    }
                                    else
                                    {
                                        DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                                        sub.DrawColors();
                                        sub.DrawRanks();
                                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_jokerWyb.png")));
                                    }
                                    table1.panel.add(table1.BG);
                                    table1.panel.repaint();
                                }
                            });
                        }
                        else if(table1.players[p_index].getCard(ind).rank=='A' && table1.as==false)
                        {
                            table1.panel.remove(table1.BG);
                            table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandWyb.png")));
                            DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                            sub.DrawColors();
                            table1.panel.add(table1.BG);
                            table1.menu[4].addMouseListener(new MouseAdapter ()
                            {
                                public void mousePressed(MouseEvent me)
                                {
                                    table1.panel.remove(table1.BG);
                                    if(me.getClickCount()%2==1)
                                    {
                                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandDis.png")));
                                        table1.BackUpDemand=table1.players[p_index].getCard(ind).color;
                                        for(int i=0; i<4; i++)
                                            table1.panel.remove(table1.colors[i]);
                                        table1.panel.remove(table1.bgDC);
                                    }
                                    else
                                    {
                                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandWyb.png")));
                                        DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                                        sub.DrawColors();
                                    }
                                    table1.panel.add(table1.BG);
                                }
                            });
                        }
                        else if(table1.players[p_index].getCard(ind).rank=='J' && table1.walet==false)
                        {
                            table1.panel.remove(table1.BG);
                            table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandWyb.png")));
                            DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                            sub.DrawRanks();
                            table1.panel.add(table1.BG);
                            table1.panel.repaint();
                            table1.menu[4].addMouseListener(new MouseAdapter ()
                            {
                                public void mousePressed(MouseEvent me)
                                {
                                    table1.panel.remove(table1.BG);
                                    if(me.getClickCount()%2==1)
                                    {
                                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandDis.png")));
                                        table1.BackUpDemand=table1.players[p_index].getCard(ind).color;
                                        for(int i=0; i<8; i++)
                                            table1.panel.remove(table1.ranks[i]);
                                        table1.panel.remove(table1.bgDR);
                                    }
                                    else
                                    {
                                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandWyb.png")));
                                        DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                                        sub.DrawRanks();
                                    }
                                    table1.panel.add(table1.BG);
                                    }
                                });
                            }
                            if(table1.peeked==true)
                            {
                                table1.peeked=false;
                                table1.peeked_B=true;
                            }
                            label.setBounds(label.getX(), table1.players[0].Y_height-40, label.getWidth(), label.getHeight());
                            table1.players[p_index].getCard(ind).ind = ind;
                            table1.Selected.add(new Card(table1.players[p_index].getCard(ind)));
                    }
                    if(table1.Selected.size()>0)
                        table1.menu[1].setIcon(new ImageIcon(MouseAdapter.class.getResource("/taliav3/img/menu_rzucDis.png")));
                }
                else if(me.getClickCount()%2==0)
                {
                    int ToBreak=-1; boolean stability=true;
                    for(int i=0; i<table1.Selected.size(); i++)
                    {
                        if(table1.players[p_index].getCard(ind).ind==table1.Selected.get(i).ind)
                            ToBreak=i;
                    }
                    if(ToBreak==0 && table1.Selected.size()>1 && table1.Selected.get(ToBreak).rank!=table1.talia.ActiveCard.rank)
                        stability=false;
                    if(stability && ToBreak!=-1)
                    {
                        table1.Selected.remove(ToBreak);
                        if( table1.Selected.size()==0 || (table1.Selected.get(0).rank!='B' && table1.Selected.get(0).rank!='J' && table1.Selected.get(0).rank!='A' ) )
                        {
                            if(table1.peeked_B==true)
                            {
                                table1.peeked=true;
                                table1.peeked_B=false;
                            }
                            table1.BackUpDemandQueue=0;
                            table1.BackUpDemand='0';
                            table1.panel.remove(table1.BG);
                            table1.panel.remove(table1.peek);
                            for(int i=0; i<4; i++)
                                table1.panel.remove(table1.colors[i]);
                            for(int i=0; i<8; i++)
                                table1.panel.remove(table1.ranks[i]);
                            try {   table1.panel.remove(table1.bgDC);   } catch(Exception e){};
                            try {   table1.panel.remove(table1.bgDR);  } catch(Exception e){};
                            try
                            {
                                for(int i=8; i<13; i++)
                                    table1.panel.remove(table1.ranks[i]);
                            }
                            catch(Exception e){};
                            table1.panel.remove(table1.menu[4]);
                            table1.menu[4] =  new JLabel();
                            Labels.setLabel(table1.menu[4], 1000, 646, 140, 52, "/taliav3/img/menu_blankDis.png");
                            table1.panel.add(table1.menu[4]);
                            table1.panel.add(table1.BG);
                            table1.panel.repaint();
                        }
                        label.setBounds(label.getX(), table1.players[0].Y_height, label.getWidth(), label.getHeight());
                    }
                    else
                    {
                        for(int i=0; i<table1.Selected.size(); i++)
                            table1.players[p_index].getCard(table1.Selected.get(i).ind).setBounds
                                    (table1.players[p_index].getCard(table1.Selected.get(i).ind).getX(),
                                    table1.players[0].Y_height,
                                    table1.players[p_index].getCard(table1.Selected.get(i).ind).getWidth(),
                                    table1.players[p_index].getCard(table1.Selected.get(i).ind).getHeight());
                        table1.Selected.clear();
                    }
                    if(table1.Selected.size()<1)
                        table1.menu[1].setIcon(new ImageIcon(MouseAdapter.class.getResource("/taliav3/img/menu_rzuc.png")));
                }
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                Cursor comp1 = label.getCursor();
                int ind = table1.GetIndex(p_index,label.getX(),table1.players[p_index].X_width);
                if(ind!=-1){
                if(( table1.players[p_index].MovePossible(ind, table1.talia) && table1.Selected.size()==0 && table1.peeked==false) ||
                    (table1.Selected.size()> 0 && (table1.Selected.get(0).rank==table1.players[p_index].getCard(ind).rank
                    || table1.players[p_index].getCard(ind).rank=='B')) || (table1.peeked==true && ind==table1.players[p_index].tPlayer.size()-1))
                    label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                else
                    label.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                if(!table1.jokers && table1.players[p_index].getCard(ind).rank=='B')
                    label.setToolTipText("Zamień jokera na kartę!");
                else
                    label.setToolTipText("");}
            }
            public void mouseExited(MouseEvent me){}
        };
    }
    /** Handling of clicking menu Rzuc/Pobierz/Sortuj.*/
    protected static MouseAdapter pickMenuAction(final String path, DrawTable table)
    {
        table1 = table;
        p_index = getCurrent(table);
        return new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                boolean joker_converted=true;
                for(int i=0; i<table1.Selected.size(); i++)
                    if(table1.Selected.get(i).rank=='B')
                        joker_converted=false;
                if(table1.Selected.size()>0 && table1.players[p_index].MovePossible(table1.Selected.get(0).ind, table1.talia) && label.getWidth()==171 && joker_converted)
                {
                    table1.players[p_index].ThrowCard(table1);
                    table1.peeked=false;
                    table1.peeked_B=false;
                    table1.ReDrawComponents(true,0);
                    MouseAdapters.blockContent(table1,1);	
                }
                else if(label.getWidth()==208)
                {
                    table1.peeked=false;
                    table1.peeked_B=false;
                    int current = getCurrent(table1);
                    if(table1.talia.get>0 && table1.players[current].QueueStop==0)
                        table1.players[p_index].peekCard(table1.talia.get, table1);
                    else
                        table1.players[p_index].peekCard(1, table1);
                }
                else if(label.getWidth()==121)
                {
                    if(me.getClickCount()%2==0)
                    {
                        table1.panel.remove(table1.BG);
                        table1.panel.repaint();
                        label.setIcon(new ImageIcon(getClass().getResource(path+"Wyb.png")));
                        label.repaint();
                        DrawTransformation sub = new DrawTransformation(table1,0,0);
                        sub.Sort();
                    }
                    else
                    {
                        label.setIcon(new ImageIcon(getClass().getResource(path+"Dis.png")));
			table1.panel.remove(table1.BG);
                        for(int i=0; i<4; i++)
                            table1.panel.remove(table1.sort[i]);
                    }
                    table1.panel.add(table1.BG);
                    table1.panel.repaint();
                }
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                boolean joker_converted=true;
                for(int i=0; i<table1.Selected.size(); i++)
                if(table1.Selected.get(i).rank=='B')
                    joker_converted=false;
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
        };
    }
    /** Handling event when card drawned from the deck can be place on top.*/
    protected static MouseAdapter pickPeeked(DrawTable table)
    {
        table1 = table;
        return new MouseAdapter ()
        {
            public void mousePressed(MouseEvent me)
            {
                System.out.println("o la boga "+table1.talia.get);
                if(table1.talia.get>1)
                    table1.players[0].getCards(table1.talia, table1.talia.get-1);
                table1.talia.get=0;
                table1.Selected.clear();
                table1.peeked=false;
                table1.peeked_B=false;
		table1.ReDrawComponents(true,0);
                table1.panel.remove(table1.BG);
                table1.panel.add(table1.BG);
                table1.status.setOpaque(true);
		NetworkAdapters.Write(table1);
                MouseAdapters.blockContent( table1,1);
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                if(table1.talia.get>1)
                    label.setToolTipText("Kliknij jeśli chcesz pobrać kolejne karty!");
            }
        };
    }
    /** Handling event when card drawned from the deck can be place on top and is Joker/Jack/Ace (adding extra sub-menus).*/
    protected static void menu_special(char rank, DrawTable table)
    {
        table1 = table;
        ind = table1.players[getCurrent(table1)].tPlayer.size()-1;
        if(rank=='B')
        {
            table1.panel.remove(table1.BG);
            DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
            sub.DrawColors();
            sub.DrawRanks();
            table1.panel.add(table1.BG);
            table1.menu[4].addMouseListener(new MouseAdapter ()
            {
                public void mousePressed(MouseEvent me)
                {
                    table1.panel.remove(table1.BG);
                    if(me.getClickCount()%2==1)
                    {
                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_jokerDis.png")));
                        for(int i=0; i<4; i++)
                            table1.panel.remove(table1.colors[i]);
                        for(int i=0; i<8; i++)
                            table1.panel.remove(table1.ranks[i]);
                        try {   table1.panel.remove(table1.bgDC);   } catch(Exception e){};
                        try {   table1.panel.remove(table1.bgDR);  } catch(Exception e){};
                        try
                        {
                            for(int i=8; i<13; i++)
                                table1.panel.remove(table1.ranks[i]);
                        }
                        catch(Exception e){};
                        table1.panel.repaint();
                    }
                    else
                    {
                        DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                        sub.DrawColors();
                        sub.DrawRanks();
                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_jokerWyb.png")));
                    }
                    table1.panel.add(table1.BG);
                    table1.panel.repaint();
                }
            });
        }
        else if(rank=='A')
        {
            table1.menu[1].setIcon(new ImageIcon(MouseAdapter.class.getResource("/taliav3/img/menu_rzucDis.png")));
            table1.panel.remove(table1.BG);
            DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
            sub.DrawColors();
            table1.panel.add(table1.BG);
            table1.menu[4].addMouseListener(new MouseAdapter ()
            {
                public void mousePressed(MouseEvent me)
                {
                    table1.panel.remove(table1.BG);
                    if(me.getClickCount()%2==1)
                    {
                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandDis.png")));
                        table1.BackUpDemand=table1.players[p_index].getCard(ind).color;
                        for(int i=0; i<4; i++)
                            table1.panel.remove(table1.colors[i]);
                        table1.panel.remove(table1.bgDC);
                    }
                    else
                    {
                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandWyb.png")));
                        DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                        sub.DrawColors();
                    }
                    table1.panel.add(table1.BG);
                }
            });
        }
        else if(rank=='J')
        {
            table1.panel.remove(table1.BG);
            table1.menu[1].setIcon(new ImageIcon(MouseAdapter.class.getResource("/taliav3/img/menu_rzucDis.png")));
            DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
            sub.DrawRanks();
            table1.panel.add(table1.BG);
            table1.panel.repaint();
            table1.menu[4].addMouseListener(new MouseAdapter ()
            {
                public void mousePressed(MouseEvent me)
                {
                    table1.panel.remove(table1.BG);
                    if(me.getClickCount()%2==1)
                    {
                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandDis.png")));
                        table1.BackUpDemand=table1.players[p_index].getCard(ind).color;
                        for(int i=0; i<8; i++)
                            table1.panel.remove(table1.ranks[i]);
                        table1.panel.remove(table1.bgDR);
                    }
                    else
                    {
                        table1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/menu_demandWyb.png")));
                        DrawTransformation sub = new DrawTransformation(table1,getCurrent(table1),ind);
                        sub.DrawRanks();
                    }
                    table1.panel.add(table1.BG);
                }
            });
        }
    }
    /** Handling event when game is over.*/
    public static MouseAdapter NewGame(DrawTable table)
    {
        table1=table;
        return new MouseAdapter()
        {
            public void mousePressed(MouseEvent me)
            {
                if(me.getClickCount()%2==1)
                {
                    table1.stop();
                    table1.table.setVisible(false);
                    System.out.println("rewers"+table1.rewersI);
                    System.out.println("talia"+table1.taliaI);
                    System.out.println("rewers"+table1.joker);
                    System.out.println("serwerpath"+table1.serwerpath);
                    System.out.println("path"+table1.path);
                    System.out.println("czy serwer"+table1.Server);
                    System.out.println("port"+(table1.port+1));
                    MainMenu main = new MainMenu(table1.rewersI,table1.taliaI,table1.joker,table1.path,table1.Server,table1.port+1,table1.serwerpath);
                    table1.table.dispose();
                }
            }
        };
    }
    public static int getCurrent(DrawTable table)
    {
        table1 = table;
        for(int i=0; i<table1.players.length; i++)
            if(table1.players[i].active==true)
                return i;
        return -1;
    }
    public static int getNext(DrawTable table)
    {
        table1 = table;
        for(int i=0; i<table1.players.length; i++)
            if(table1.players[i].active==false)
                return i;
        return -1;
    }
    /** Blocking menu Rzuc/Pobierz karte/Sortuj.*/
    public static void blockContent(DrawTable table, int from)
    {
        table1 = table;
        int index=1;
        if(table1.Server==true)
            index=0;
        if(from==-1)
        {
            table1.panel.remove(table1.BG);
            table1.panel.add(table1.BG);
            table1.status.setOpaque(true);
            return;
        }

        String paths[] = {"rzuc", "pobierz", "sortuj", "blank"};
        for(int i=from; i<4; i++)
        {
            table1.menu[i].setIcon(new ImageIcon(MouseAdapter.class.getResource("/taliav3/img/menu_"+paths[i-1]+".png")));
            table1.menu[i].repaint();
        }
        for(int i=from; i<5; i++)
            if(table1.menu[i].getMouseListeners().length>0)
                table1.menu[i].removeMouseListener(table1.menu[i].getMouseListeners()[0]);
        if(from==1 && table1.koniec_gry==false)
        {
            for(int i=0; i<table1.players[index].getPlayerCardsAmount(); i++)
                if(table1.players[index].getCard(i).getMouseListeners().length>0)
                {
                    for(int j=table1.players[index].getCard(i).getMouseListeners().length-1; j>=0 ; j--)
                        table1.players[index].getCard(i).removeMouseListener(table1.players[index].getCard(i).getMouseListeners()[j]);
                    table1.players[index].getCard(i).repaint();
                }
            table1.panel.remove(table1.status);
            table1.panel.remove(table1.BG);
            table1.status.setBounds(2, 700-54, 600, 52);
            table1.status.setOpaque(true);
            table1.status.setBackground(Color.red);
            table1.status.setForeground(Color.white);
            Font font1 = new Font("Century Gothic", Font.PLAIN, 30);
            table1.status.setFont(font1);
            table1.status.setText("  Czekaj na ruch przeciwnika");
            table1.panel.add(table1.status);
            table1.panel.add(table1.BG);
        }
    }
    /** Handling events in MainMenu.*/
    public static MouseAdapter pickMainMenu (MainMenu menu, final String path)
    {
        menu1 = menu;
        return new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.getIcon().getClass().getResource(path);
                if(label.getName().equals("nowa") || label.getName().equals("heartN"))
                    menu1.ServOrClient();
                else if(label.getName().equals("opcje") || label.getName().equals("heartO"))
                    menu1.options();
                else if(label.getName().equals("koniec") || label.getName().equals("heartD"))
                    System.exit(0);
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setIcon(new ImageIcon(getClass().getResource(path+"BUMP.png")));
                Cursor comp1 = label.getCursor();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

                if(label.getName().equals("nowa"))
                    menu1.menu[1].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/heartBUMP.png")));
                else if(label.getName().equals("heartN"))
                    menu1.menu[0].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/nowaBUMP.png")));
                else if(label.getName().equals("opcje"))
                    menu1.menu[3].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/heartBUMP.png")));
                else if(label.getName().equals("heartO"))
                    menu1.menu[2].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/opcjeBUMP.png")));
                else if(label.getName().equals("koniec"))
                    menu1.menu[5].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/heartDBUMP.png")));
                else if(label.getName().equals("heartD"))
                    menu1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/koniecBUMP.png")));
                label.repaint();
            }
            public void mouseExited(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setIcon(new ImageIcon(getClass().getResource(path+".png")));
                if(label.getName().equals("nowa"))
                    menu1.menu[1].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/heart.png")));
                else if(label.getName().equals("heartN"))
                    menu1.menu[0].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/nowa.png")));
                else if(label.getName().equals("opcje"))
                    menu1.menu[3].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/heart.png")));
                else if(label.getName().equals("heartO"))
                    menu1.menu[2].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/opcje.png")));
                else if(label.getName().equals("koniec"))
                    menu1.menu[5].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/heartD.png")));
                else if(label.getName().equals("heartK"))
                    menu1.menu[4].setIcon(new ImageIcon(getClass().getResource("/taliav3/img/koniec.png")));
                label.repaint();
            }

        };
    }
    /** Handling events when in help mode.*/
    public static MouseAdapter helpNavigateNext(int stage, MainMenu menu)
    {
        menu1=menu;
        Stage=stage;
        return new MouseAdapter()
        {
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                Cursor comp1 = label.getCursor();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
            public void mouseClicked(MouseEvent me)
            {
                if(Stage<5)
                    Stage++;
                else
                    Stage--;
                menu1.help(Stage);
            }
        };
    }
    public static MouseAdapter helpNavigatePrevious(int stage, MainMenu menu)
    {
        menu1=menu;
        Stage=stage;
        return new MouseAdapter()
        {
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                Cursor comp1 = label.getCursor();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
            public void mouseClicked(MouseEvent me)
            {
                Stage--;
                menu1.help(Stage);
            }
        };
    } 
    public static MouseAdapter getFrameLocation (DrawTable table)
    {
        table1 = table;
        return new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                table1.point.x = e.getX();
                table1.point.y = e.getY();
            }
        };
    }
    public static MouseMotionAdapter moveFrame (DrawTable table)
    {
        table1 = table;
        return new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {
                Point p = table1.table.getLocation();
                table1.table.setLocation(p.x + e.getX() - table1.point.x, p.y + e.getY() - table1.point.y);
            }
        };
    }
    public static MouseAdapter getFrameLocation (MainMenu menu)
    {
        menu1 = menu;
        return new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                menu1.point.x = e.getX();
                menu1.point.y = e.getY();
            }
        };
    }
    public static MouseMotionAdapter moveFrame (MainMenu menu)
    {
        menu1 = menu;
        return new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {
                Point p = menu1.MainW.getLocation();
                menu1.MainW.setLocation(p.x + e.getX() - menu1.point.x, p.y + e.getY() - menu1.point.y);
            }
        };
    }
    public static MouseAdapter minimizeFrame(JFrame frame1)
    {
        frame=frame1;
        return new MouseAdapter()
        {
            public void mousePressed(MouseEvent me)
            {
                frame.setState(frame.ICONIFIED);
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                Cursor comp1 = label.getCursor();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/minOver.png")));
                label.setToolTipText("Kliknij by zminimalizować aplikację!");
            }
            public void mouseExited(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/min.png")));
            }
        };
    }
    public static MouseAdapter closeFrame()
    {
        return new MouseAdapter()
        {
            public void mousePressed(MouseEvent me)
            {
                System.exit(0);
            }
            public void mouseEntered(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                Cursor comp1 = label.getCursor();
                label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/closeOver.png")));
                label.setToolTipText("Kliknij by zamknąć aplikację!");
            }
            public void mouseExited(MouseEvent me)
            {
                JLabel label = (JLabel) me.getComponent();
                label.setIcon(new ImageIcon(getClass().getResource("/taliav3/img/close.png")));
            }
        };
    }
    public static void unlockContent(DrawTable table)
    {
        table1 = table;        
        table1.panel.remove(table1.BG);
        table1.status.setBounds(2, 700-54, 600, 52);
        table1.status.setOpaque(true);
        table1.status.setBackground(Color.green);
        table1.status.setForeground(Color.black);
        Font font1 = new Font("Century Gothic", Font.PLAIN, 30);
        table1.status.setFont(font1);
        table1.status.setText("  Twoj ruch");
        try
        {
            table1.panel.add(table1.status);
        }
        catch(Exception e)
        {}
        table1.panel.add(table1.BG);         
    }
}

